<template>
	<view>
		<!-- 未完成日常体检 -->
		<block v-if="undone==0">
			<view class="topIsShow">
				<image :src="benpng" mode="aspectFit" class="bann"></image>
			</view>
			<block v-if="goodList.length<=0">
				<yangs :isShow="1" :isSet="1" ></yangs>
			</block>
			<block v-else>
				<badness :list="goodList" @func="getMsgFormSon" :toDay="toDay" :isServerShow="isServerShow" :listGoods="listGoods"
				 :isSelect="isSelect" :numSone="numSone"></badness>
			</block>
		</block>
		<!-- 已完成日常体检 -->
		<block v-else>
			<view class="banner ">
				<swiper class="swiper" :indicator-dots="false" :autoplay="false" interval="5000" duration="600" previous-margin="16px"
				 next-margin="20px" @change="getChange" :current="current">
					<swiper-item v-for="(item,index) in list" :key="index">
						<block v-if="item.IsShow==1">
							<block v-if="item.WeekId<5">
								<image :src="bannerpng" mode="aspectFill" class="imags"></image>
								<view class="bannerTexe ">
									<view class="topText dis dis2">
										<image src="../../static/img/title.png" mode="aspectFit" class="title" @click="showRules"></image>
									</view>
									<view class="item dis dis4">
										<block v-for="(i,index1) in item.data" :key="index1">
											<view class="items contenr">
												<image src="../../static/img/bgs.png" mode="aspectFit" class="bgImg" v-if="i.IsConflict==0"></image>
												<image src="../../static/img/bgs1.png" mode="aspectFit" class="bgImg" v-if="i.IsConflict==1"></image>
												<text class="bgsText contenr">{{i.TaskTitle}}</text>
											</view>
										</block>
									</view>
								</view>
							</block>
							<block v-if="item.WeekId==5">
								<image :src="bannerpng" mode="aspectFill" class="imags"></image>
								<view class="bannerTexe ">
									<view class="topText topText_1 dis dis2">
										<image src="../../static/img/wenzi.png" mode="aspectFit" class="title title_123" @click="showRules"></image>
									</view>
									<view class="item dis dis4">
										<block v-for="(i,index1) in item.data" :key="index1">
											<view class="itemsChu contenr">
												<image src="../../static/img/bgschu.png" mode="aspectFit" class="bgImgChu"></image>
												<text class="bgsText contenr">{{i.TaskTitle}}</text>
											</view>
										</block>
									</view>
								</view>
							</block>
						</block>
						<block v-if="item.IsShow==0">
							<view class=" bannerTexe_1  dis dis5 dis1 dis2">
								<image src="../../static/img/suo.png" mode="aspectFit" class="suo"></image>
								<text class="secondTitle">{{item.secondTitle}}</text>
							</view>
						</block>
					</swiper-item>
				</swiper>
			</view>
			<badness :list="goodList" @func="getMsgFormSon" :toDay="toDay" :isServerShow="isServerShow" :listGoods="listGoods"
			 :isSelect="isSelect" :numSone="numSone" :future="future"></badness>
		</block>
		<!-- 背景图片 -->
		<view class="bgs dis  dis2 dis5" v-if="bgShow">
			<view class="contnImg dis dis1 dis2">
				<image :src="zhuanpng" mode="aspectFill" class="zhuan"></image>
				<image :src="xiao" mode="aspectFit" class="xiao"></image>
			</view>
			<view class="del">
				<image src="../../static/img/del.png" mode="aspectFit" class="dels" @click="dels"></image>
			</view>
		</view>
		<view class="showRule dis dis5 dis2" v-if="rule">
			<view class="kongsTo"></view>
			<view class="rule dis dis5 dis1 dis2">
				<image :src="rulepng" mode="aspectFit" class="ruleImg"></image>
				<image src="../../static/img/Ikon.png" mode="aspectFit" class="Ikon" @click="delsRule"></image>
			</view>

		</view>

		<view>
			<!-- <tar-bar :indexTo="2"></tar-bar> -->
			<!-- <tar-bar :indexTo="1"></tar-bar> -->
		</view>
	</view>
</template>

<script>
	let app = getApp().globalData;
	import config from '@/common/js/config.js';
	import yangs from '../../components/yangs.vue';
	import badness from '../../components/badness.vue';
	import util from '../../common/js/util.js';
	import tarBar from '@/components/tarbar.vue'
	export default {
		components: {
			'yangs': yangs,
			'badness': badness,
			'tarBar': tarBar
		},
		data() {
			return {
				undone: app.user.isexamiate || 0, //是否完成日常体检
				// undone:1,
				current: -1, //当前所在的滑块
				list: [],
				goodList: [], //这个是所有的打卡
				good: {}, //有打卡任务的数据对象
				is: 1,
				toDay: '本周',
				bgShow: false, //是否显示挑战成功或是失败的信息
				xiao: `${config.imgUrl}xiao.png`, //图片
				objData: {},
				rule: false, //规则显示
				isServerShow: 0,
				listGoods: [], //已完成的
				isSelect: 1,
				numSone: 0,
				weekid: 0, //当前周的第几个滑块
				currentInfo: app.currentInfo,
				// imgBase64: '',
				future:1,
				zhuanpng:`${config.imgUrl}zhuan.png`,
				benpng:`${config.imgUrl}ben.png`,
				bannerpng:`${config.imgUrl}banner.png`,
				rulepng:`${config.imgUrl}rule.png`,
			}
		},
		methods: {
			// 获取轮播图的滑动数
			getChange(e) {
				// console.log(e)
				let index = this.weekid;
				console.log('index',index)
				let current = e.detail.current;
				console.log(current)
				if (current > index && current !=0) { //如果当前的滑块大于周则：
					console.log('ssssss')
					this.current = index;
					this.future = 0;
					this.goodList=[];
					return
				}
				this.future = 1;
				let that = this;
				this.current = current; //赋值
				this.currentInfo = current; //用户滑到哪个模块
				app.currentInfo = current; //重新设置全局变量currentInfo;
				let good = this.good;
				this.goodList = good[current + 1];
				console.log('this.goodList', this.goodList)
				if(that.goodList) {
					this.setIsover(this.goodList)
					this.isSelect = 1;
					this.setToDay(current);
					this.$forceUpdate();
					let listTo = this.goodList;
					let list = []
					let s = 0;
					for (let i = 0; i < listTo.length; i++) {
						if (listTo[i].isover == 1) {
							s = s + 1;
							list.push(listTo[i])
						}
					}
					if (s == listTo.length) {
						that.numSone = 3;
						that.listGoods = list;
					} else {
						that.numSone = 0;
						that.listGoods = list;
					}
				}
				// that.listGoods = listTo;

			},
			getMsgFormSon(data) {
				console.log('data', data)
				if (data) {
					this.getHomeData()
				}
			},
			getHomeData() {
				let that = this;
				this.$http._post('yang/task.do').then(res => {
					console.log(res)
					if (res.data.issuccess == 1) {
						let {
							data
						} = res.data;
						let tasklist = data.tasklist;
						console.log('data.firstdate',data.firstdate  )
						let dateBoj= data.firstdate  || '2020-10-01';
						let firstdate = new Date(dateBoj).getDate() ;
						that.firstdate = firstdate;
						that.objData=that.getDateM();
						console.log('data.weekid',data.weekid);
						// console.log()
						let list = [];
						let map = {},
							dest = [];
						for (let i = 0; i < tasklist.length; i++) {
							let ai = tasklist[i];
							if (!map[ai.WeekId]) {
								dest.push({
									WeekId: ai.WeekId,
									TaskTitle: ai.TaskTitle,
									IsShow: ai.IsShow,
									secondTitle: ai.SecondTitle || '8月8号解锁',
									data: [ai]
								});
								map[ai.WeekId] = ai;
							} else {
								for (let j = 0; j < dest.length; j++) {
									let dj = dest[j];
									if (dj.WeekId == ai.WeekId) {
										dj.data.push(ai);
										break;
									}
								}
							}

						}
						console.log(dest)
						that.list = dest;
						that.good = data.actionlist;
						// that.setToDay(that.current)
						console.log('that.objData',that.objData)
						if (data.weekid == 5) {
							let obj = that.objData;
							let listData = that.list;
							listData.push(obj);
							that.list = listData;
							that.goodList = [];
							console.log('listData',listData)
							// that.current==4;
						}
						let actionlist = data.actionlist;
						
						let goodList = actionlist[data.weekid];
						that.isSelect = 1;
						// 判断用户滑到当前那个模块
						if (that.currentInfo == -1) {
							console.log('用户没有滑动')
							that.goodList = actionlist[data.weekid];
							that.current = data.weekid - 1; //判断当前是第几周  isShow 当前周是否显示
							that.weekid = data.weekid - 1;
						} else {
							console.log('用户滑动了其中模块是第', that.currentInfo)
							that.goodList = actionlist[that.currentInfo + 1];
							that.current = that.currentInfo; //判断当前是第几周  isShow 当前周是否显示
							// that.weekid = that.currentInfo;
							goodList = actionlist[that.currentInfo + 1]; //重新赋值，解决bug
						}
						that.setToDay(that.current);
						if (data.weekid != 5) {
							that.setIsover(that.goodList)
						}
						if (app.user.isexamiate != 0) {
							that.undone = 1;
						}
						let s = 0;
						let listGoods = [];
						if (data.weekid != 5) {
							for (let j = 0; j < goodList.length; j++) {
								if (goodList[j].isover == '1') {
									listGoods.push(goodList[j])
									s = s + 1;
								}
							}
							if (s == goodList.length) {
								that.toDay = "以下就当没发生",
									that.isSelect = -1;
								that.numSone = 3; //是否全部完成
							} else {
								that.numSone = 0; //是否全部完成
								console.log('numSone', that.numSone)

							}
						}else{
							listGoods=goodList;
						}
						that.listGoods = listGoods;
						this.$forceUpdate();
						console.log('goodList', goodList)

					}
				})
			},
			dels() {
				this.bgShow = false
			},
			// 判断当前时间
			getDateM() {
				let firstdate = this.firstdate ;
				console.log('firstdate',firstdate)
				if(!firstdate){
					return
				}
				let obj = {
					WeekId: '',
					TaskTitle: '',
					IsShow: '',
					data: []
				};
				if (firstdate < 29) {
					obj.WeekId = 5;
					obj.TaskTitle = '是否还有没补救的任务呀，快回顾一下吧~ ';
					obj.IsShow = 1;
					obj.data.push({
						WeekId: 5,
						TaskTitle: '是否还有没补救的任务呀，快回顾一下吧~ ',
						IsShow: 1
					})
				} else if (firstdate >= 29) {
					obj.WeekId = 5;
					obj.TaskTitle = '没赶上本月任务的班车，不过没关系，请期待下个月的任务卡吧!';
					obj.IsShow = 1;
					obj.data.push({
						WeekId: 5,
						TaskTitle: '没赶上本月任务的班车，不过没关系，请期待下个月的任务卡吧!',
						IsShow: 1
					})
				}
				return obj;
			},
			// 规则显示
			showRules() {
				this.rule = true;
			},
			delsRule() {
				this.rule = false;
			},
			setToDay(date) {
				// 计算当前月有多少天
				let that = this;
				let dateTime = new Date()
				let dates = util.fmdata(dateTime);
				let time = new Date(dates.Y, dates.M, 0).getDate()
				console.log('time', time);
				console.log(`${dates.M}.01-${dates.M}.07`)
				if (date == 0) {
					that.toDay = `${dates.M}.01-${dates.M}.07`
				} else if (date == 1) {
					that.toDay = `${dates.M}.08-${dates.M}.14`
				} else if (date == 2) {
					that.toDay = `${dates.M}.15-${dates.M}.21`
				} else if (date == 3) {
					that.toDay = `${dates.M}.22-${dates.M}.28`
				} else {
					let num = time - 28;
					that.toDay = `${dates.M}.29-${dates.M}.${29+num-1}`
				}
			},
			setIsover(list) {
				let s = 0;
				let is = 0; /*0:未完成所有的打卡记录；1已完成所有的记录*/
				for (let i = 0; i < list.length; i++) {
					if (list[i].isover == 1) {
						s = s + 1;
					}
				}
				if (s == list.length) {
					is = 1
				};
				this.isServerShow = is;
			},
			

		},
		onLoad() {
			console.log(app)
			if (app.user.ischallengesuccess == 1) {
				this.bgShow = true
			} else if (app.user.ischallengesuccess == 2) {
				this.bgShow = true;
				this.xiao = `${config.imgUrl}ku.png`;
			} else {
				return
			}

		},
		onShow() {
			this.getHomeData();
		
			app.dateTime = 'young';
			this.currentInfo = app.currentInfo;

		},
		// onPullDownRefresh() {
		// 	console.log('refresh');
		// 	setTimeout(function() {
		// 		uni.stopPullDownRefresh();
		// 	}, 1000);
		// },
		onShareAppMessage: function(e) {
			let title = `Young养`
			return {
				title: title,
				path: `/pages/index/index`
			}
		},
		
		onShareTimeline:function(){
			let title = `拯救罪恶感，一起朋克养生！`;
			// let uid = uni.getStorageSync('uid');
			// let reportid = app.reportid;
			return {
				title: title,
				path:'/pages/index/index'
			}
		}
	}
</script>

<style lang="scss">
	@import "../../common/css/flex.css";
	@import "../../common/css/index.scss";

	page {
		width: 100%;
		height: 100%;
		background-color: #F8F8F8;
	}

	.topIsShow {
		width: 688upx;
		height: 336upx;
		// border: 1px solid red;
		margin: auto;

		.bann {
			width: 100%;
			height: 100%;
		}
	}

	.banner,
	swiper {
		width: 750upx;
		height: 336upx;
		background: linear-gradient(180deg, #FFFFFF 0%, #F8F8F8 100%);

		.imags {
			width: 688upx;
			height: 336upx;
			position: relative;
		}

		.bannerTexe {
			width: 610upx;
			height: 290upx;
			position: absolute;
			top: 0;
			left: 25upx;
			top: 10upx;

			.topText {
				height: 92upx;
				width: 100%;
				margin-bottom: 28upx;

				.title {
					width: 309upx;
					height: 42upx;
					margin-left: 25upx;
				}
				.title_123{
					margin-left: 0;
				}
			}
			.topText_1{
				margin-bottom: 20upx;
			}

			.item {
				width: 100%;
				height: 170upx;
				margin-left: 15upx;

				.items {
					width: 200upx;
					height: 150upx;
					position: relative;

					.bgImg {
						width: 100%;
						height: 100%;
					}


					.bgsText {
						font-size: 30upx;
						font-family: $typeface;
						font-weight: 500;
						color: #918460;
						position: absolute;
						margin: 0 5upx;
						width: 150upx;
						// text-align: center;

					}
				}

				.itemsChu {
					width: 592upx;
					height: 160upx;
					position: relative;
					margin-left: 5upx;

					.bgImgChu {
						width: 592upx;
						height: 160upx;
					}

					.bgsText {
						font-size: 30upx;
						font-family: $typeface;
						font-weight: 500;
						color: #918460;
						position: absolute;
						// margin: 0 35upx;
						width: 61%;
						text-align: center;
					}
				}
			}

		}

		.bannerTexe_1 {
			width: 690upx;
			height: 329upx;
			position: absolute;
			top: 0;
			// left: 25upx;
			// top: 10upx;
			background-image: url(http://r.punkyangsheng.com/common/img/xcx_top_bg.png);
			background-size: 100% 100%;

			.suo {
				width: 64upx;
				height: 72upx;
				margin-bottom: 16upx;
			}

			.secondTitle {
				font-size: 40upx;
				font-family: $typeface;
				font-weight: 500;
				color: #FFFFFF;
			}
		}
	}

	.bgs {
		position: fixed;
		width: 100%;
		height: 100%;
		z-index: 999;
		background: rgba(0, 0, 0, 0.7);
		bottom: 0;
		overflow: hidden;

		.contnImg {
			width: 580upx;
			height: 835upx;
			// border: 1px solid red;
			position: relative;

			.zhuan {
				width: 580upx;
				height: 580upx;
				position: absolute;
				animation: box-ani 4s infinite linear;
			}

			@keyframes box-ani {
				from {
					transform: rotate(0)
				}

				to {
					transform: rotate(360deg)
				}
			}

			.zhuang {
				width: 580upx;
				height: 580upx;
				position: absolute;
			}

			.xiao {
				width: 580upx;
				height: 580upx;
				position: absolute;
			}
		}

		.del {
			width: 72upx;
			height: 72upx;
			margin-top: 50rpx;
			.dels {
				width: 72upx;
				height: 72upx;
			}
		}

	}

	.showRule {
		position: fixed;
		width: 100%;
		height: 100%;
		z-index: 999;
		background: rgba(0, 0, 0, 0.7);
		bottom: 0;
		overflow: hidden;

		.kongsTo {
			width: 100%;
			height: 60upx;
		}

		.rule {
			width: 666upx;
			height: 825upx;
			position: relative;

			.ruleImg {
				width: 666upx;
				height: 825upx;
				border-radius: 24rpx;

			}

			.Ikon {
				position: absolute;
				width: 264upx;
				height: 88upx;
				top: 690upx;
				left: 195upx;
				font-size: 32upx;
			}
		}

		.del {
			width: 72upx;
			height: 72upx;
			margin-top: 64upx;

			.dels {
				width: 72upx;
				height: 72upx;
			}
		}
	}
</style>
